package com.jjong.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.jjong.dto.BoardDto;
import com.jjong.dto.FrequestDto;
import com.jjong.dto.FriShowDto;
import com.jjong.dto.FriendDto;
import com.jjong.dto.MembersDto;
import com.jjong.dto.MessageDto;
import com.jjong.dto.OtherMemDto;
import com.jjong.dto.PhotosDto;
import com.jjong.dto.WriteBoardDto;
import com.jjong.ibatis.SqlDaoSuport;

public class JjongBoardManager extends SqlDaoSuport implements IjjongBoard{

	
	@Override
	public int checkEmail(String email) {
		int count = 0;
		try {
			count = (Integer) this.getSqlMapClient().queryForObject("checkEmail",email);
		} catch (SQLException e) {
			System.out.println("checkEmail : "+e);
		}
		return count;
	}

	
	@Override
	public int addMembers(MembersDto dto) {
		int result=1;
		try {
			this.getSqlMapClient().insert("addMembers", dto);
		} catch (SQLException e) {
			result=0;
			System.out.println("addMembers : "+e);
		}
		return result;
	}
	
	@Override
	public int addMembers2(MembersDto dto) {
		int result=1;
		try {
			this.getSqlMapClient().insert("addMembers2", dto);
		} catch (SQLException e) {
			result=0;
			System.out.println("addMembers : "+e);
		}
		return result;
	}

	@Override
	public MembersDto loginMembers(MembersDto dto) {
		MembersDto mdto = null;
		try {
			mdto=(MembersDto) this.getSqlMapClient().queryForObject("loginMembers",dto);
		} catch (SQLException e) {
			System.out.println("loginMembers : "+e);
		}
		return mdto;
	}

	@Override
	public int writeprofile(MembersDto dto) {
		int count=1;
		try {
			this.getSqlMapClient().update("writeprofile",dto);
		} catch (SQLException e) {
            count=0;
            System.out.println("writeprofile : "+e);
		}

		return count;
	}

	@Override
	public MembersDto getJjongUser(String email) {
		MembersDto gdto=null;
		try {
			gdto=(MembersDto)this.getSqlMapClient().queryForObject("getJjongUser",email);
		} catch (SQLException e) {
			System.out.println("getJjongUser : "+e);
		}
		return gdto;
	}


	@Override
	public ArrayList<MembersDto> memsearch(String name) {
		ArrayList<MembersDto> list=null;
		try {
			list=(ArrayList<MembersDto>) this.getSqlMapClient().queryForList("searchUser",name);
		} catch (SQLException e) {
			System.out.println("memsearch : "+e);
		}
		
		return list;
	}
	
	@Override
	public int deleteUser(MembersDto dto) {
		int count=1;
		try {
			this.getSqlMapClient().delete("deletelast",dto);
		} catch (SQLException e) {
			count=0;
			System.out.println("deleteUser : "+e);
		}
		return count;
	
	}
	@Override
	public ArrayList<BoardDto> getAllBoards() {
		ArrayList<BoardDto> gdto=null;
		try {
	      gdto=(ArrayList<BoardDto>)this.getSqlMapClient().queryForList("getAllBoards");
		} catch (SQLException e) {
			System.out.println("getAllBoards : "+e);
		}
		return gdto;
	}

	
	@Override
	public int writeBoard(WriteBoardDto dto) {
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("membername", dto.getMembername());
		maps.put("content", dto.getContent());
		maps.put("confirm", dto.getConfirm());
		maps.put("email", dto.getEmail());
		maps.put("boardowner", dto.getBoardowner());
		maps.put("profileimg", dto.getProfileimg());
		int count=1;
		try {
			this.getSqlMapClient().insert("writeBoard",maps);
		} catch (SQLException e) {
			count=0;
			System.out.println("writeBoard : "+e);
		}
		return count;
	}

	@Override
	public ArrayList<BoardDto> getUserBoards(String email) {
		ArrayList<BoardDto> list= new ArrayList<BoardDto>();
		try {
			list = (ArrayList<BoardDto>)this.getSqlMapClient().queryForList("getUserBoards", email);
		} catch (SQLException e) {
			System.out.println("getUserBoards : "+e);
		}
		return list;
	}
	
	
	@Override
	public ArrayList<BoardDto> getUserBoardsPage(String email, int page) {
		ArrayList<BoardDto> list = null;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("boardowner", email);
		map.put("page", page);
		
		try {
			list = (ArrayList<BoardDto>)getSqlMapClient().queryForList("getUserBoardsp",map);
		} catch (SQLException e) {
			System.out.println("getUserBoardsp : "+e);
		}
		return list;
	}
	
	@Override
	public ArrayList<BoardDto> getUserBoardspaging(String email, int page) {
		ArrayList<BoardDto> list = null;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("boardowner", email);
		map.put("page", page);
		
		try {
			list =(ArrayList<BoardDto>)getSqlMapClient().queryForList("getUserBoardspaging",map);
		} catch (SQLException e) {
			System.out.println("getUserBoardspaging : "+e);
		}
		return list;
	}
	
	@Override
	public int getCountUserBoard(String email) {
        int count=-1;
        try {
			count=(Integer)this.getSqlMapClient().queryForObject("countUserBoard",email);
		} catch (SQLException e) {
			System.out.println("countUserBoard : "+e);
		}
		return count;
	}
	
	

	@Override
	public BoardDto getBoard(int boardseq) {
		BoardDto dto=null;
		try {
			dto=(BoardDto)this.getSqlMapClient().queryForObject("getBoard",boardseq);
		} catch (SQLException e) {
			System.out.println("getBoard : "+e);
		}
		return dto;
	}

	@Override
	public int reply(WriteBoardDto dto) {
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("membername", dto.getMembername());
		maps.put("content", dto.getContent());
		maps.put("parent", dto.getParent());
		maps.put("confirm", dto.getConfirm());
		maps.put("email", dto.getEmail());
		int count=1;
		try {
			this.getSqlMapClient().insert("reply",maps);
		} catch (SQLException e) {
			count=0;
			System.out.println("reply : "+e);
		}
		return count;
	}

	
	@Override
	public int reply2(WriteBoardDto dto) {
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("membername", dto.getMembername());
		maps.put("content", dto.getContent());
		maps.put("parent", dto.getParent());
		maps.put("confirm", dto.getConfirm());
		maps.put("email", dto.getEmail());
		int count=1;
		try {
			this.getSqlMapClient().insert("reply2",maps);
		} catch (SQLException e) {
			count=0;
			System.out.println("reply2 : "+e);
		}
		return count;
	}

	@Override
	public int addFile(MembersDto mDto) {
		int count=1;
		
		try {
			this.getSqlMapClient().update("fileup",mDto);
		} catch (SQLException e) {
			count=-1;
			System.out.println("addFile : "+e);
		}
		return count;
	}

	@Override
	public MembersDto findpasswd(String email) {
		MembersDto dto=new MembersDto();
		   try {
		    dto=(MembersDto)this.getSqlMapClient().queryForObject("findpasswd", email);
		   } catch (SQLException e) {
		    System.out.println("findpasswd : "+e);
		   }
		  return dto; 
	}

	@Override
	public OtherMemDto memfind(int memseq) {
		OtherMemDto odto = new OtherMemDto(); 
		try {
			 odto = (OtherMemDto)this.getSqlMapClient().queryForObject("memfind",memseq);
		} catch (SQLException e) {
			System.out.println("memfind : "+e);
		}
		return odto;
	}


	@Override
	public ArrayList<PhotosDto> getPhotoList(String email) {
		ArrayList<PhotosDto> plist=null;
		try {
			plist=(ArrayList<PhotosDto>)this.getSqlMapClient().queryForList("getPhotoList",email);
			//System.out.println("getPhotoList="+plist);
		} catch (SQLException e) {
			System.out.println("getPhotoList : "+e);
		}
		return plist;
		
	}

	@Override
	public int delBoard(int boardseq) {
		int count=1;
		try {
			this.getSqlMapClient().delete("delboard",boardseq);
		} catch (SQLException e) {
			count=0;
			System.out.println("delboard : "+e);
		}
		return count;
	}

	@Override
	public int frequest(FrequestDto fdto) {
		int count=1;
		try {
			this.getSqlMapClient().insert("friendreq",fdto);
		} catch (SQLException e) {
			count=0;
			System.out.println("frequest : "+e);
		}
		return count;
	}

	@Override
	public ArrayList<FriShowDto> fRequestList(String email) {
		ArrayList<FriShowDto> list = null;
		try {
			list = (ArrayList<FriShowDto>)this.getSqlMapClient().queryForList("frshow", email);
		} catch (SQLException e) {
			System.out.println("frequestshow : "+e);
		}
		return list;
	}
	
	@Override
	public ArrayList<FriShowDto> fRequestListPage(String email , int page) {
		ArrayList<FriShowDto> lists = null;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("email", email);
		map.put("page", page);
		
		try {
			lists =(ArrayList<FriShowDto>)getSqlMapClient().queryForList("frshowpage",map);
		} catch (SQLException e) {
			System.out.println("frshow : "+e);
		}
		return lists;
	}
	


	@Override
	public FrequestDto getFriendReq(FrequestDto fdto) {
		FrequestDto dto = null;
		
		try {
			dto = (FrequestDto) this.getSqlMapClient().queryForList("getfrireq", fdto);
		} catch (SQLException e) {
			System.out.println("getfriendreq : "+e);
		}
		return dto;
	}

	@Override
	public int friendAccept(FrequestDto fdto) {
		
		int count =1;
		try {
			this.getSqlMapClient().update("friaccept",fdto);
		} catch (SQLException e) {
			count = 0;
			System.out.println("friendaccept : "+e);
		}
		return count;
	}

	@Override
	public int friAcceptAfter(FrequestDto fdto) {
		int count = 1;	
			try {
				this.getSqlMapClient().insert("friacceptafter",fdto);
			} catch (SQLException e) {
				count = 0;
				System.out.println("friAcceptAfter : "+e);
			}
		return count;
	}

//	@Override
//	public ArrayList<FriendDto> friendListPage(String email, int page) {
//		ArrayList<FriendDto> list = null;
//		Map<String, Object> map=new HashMap<String, Object>();
//		map.put("email", email);
//		map.put("page", page);
//		
//		try {
//			list =(ArrayList<FriendDto>)getSqlMapClient().queryForList("friendshowpage",map);
//		} catch (SQLException e) {
//			System.out.println("friendshowpage : "+e);
//		}
//		return list;
//	}
	

	
	
	@Override
	public ArrayList<FriendDto> friendList(String email) {
		ArrayList<FriendDto> list = null;
			try {
				list = (ArrayList<FriendDto>) this.getSqlMapClient().queryForList("friendshow",email);
			} catch (SQLException e) {
				System.out.println("friendList : "+e);
			}
		return list;
	}

	@Override
	public int reqCheck(FrequestDto fdto) {
		int dto = 0;
		try{
			dto  = (Integer) this.getSqlMapClient().queryForObject("requestcheck",fdto);
		} catch (SQLException e) {
			System.out.println("reqCheck : "+e);
		}
		return dto;
	}

	@Override
	public int fReqRefuse(FrequestDto fdto) {
		int count = 1;
			try {
				count = this.getSqlMapClient().delete("freqrefuse", fdto);
			} catch (SQLException e) {
				count=0;
				System.out.println("fReqRefuse");
			}
		return count;
	}


	@Override
	public int getCount(String email) {
		int count = -1;		
		try {
			count= (Integer)this.getSqlMapClient().queryForObject("countFrenid",email);
		} catch (SQLException e) {
			//count=0;
		    System.out.println("countFrend : "+e);
		}		
		return count;
	}
	
	@Override
	public int getOurBoardsCount(String email) {
		int count =-1;
		try {
			count = (Integer)this.getSqlMapClient().queryForObject("countourboards",email);
		} catch (SQLException e) {
			System.out.println("countourboards : "+e);		
		}


		return count;
	}


	@Override
	public int getCountFriend(String email) {
		int count = -1;
		try {
			count=(Integer)this.getSqlMapClient().queryForObject("countFrenids",email);
		} catch (SQLException e) {
			System.out.println("countFrenids : "+e);
		}
		return count;
	}



	@Override
	public int friDelete(FrequestDto fdto) {
		int count = 1;
		try {
			this.getSqlMapClient().delete("fridelete",fdto);
		} catch (SQLException e) {
			count = 0;
			System.out.println("friDelete : "+e);
		}
		return count;
	}

	@Override
	public int friDelete2(FrequestDto fdto) {
		int count = 1;
		try {
			this.getSqlMapClient().delete("fridelete2",fdto);
		} catch (SQLException e) {
			count = 0;
			System.out.println("friDelete2 : "+e);
		}
		return count;	}

	@Override
	public ArrayList<BoardDto> getOurBoard(String email) {
		ArrayList<BoardDto> list = null;
		try {
			list = (ArrayList<BoardDto>) this.getSqlMapClient().queryForList("ourboard",email);
		} catch (SQLException e) {
			System.out.println("getOurBoard : "+e);
		}
		return list;
	}
	
	@Override
	public ArrayList<BoardDto> getOurBoardPage(String email, int page) {
		ArrayList<BoardDto> list=null;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("email", email);
		map.put("page", page);
		
		try {
			list=(ArrayList<BoardDto>)getSqlMapClient().queryForList("ourboardpage",map);
		} catch (SQLException e) {
			System.out.println("getOurBoard : "+e);
		}
		
		return list;
	}


	@Override
	public int hideBoard(int bseq) {
		int count =1;
		try {
			this.getSqlMapClient().update("hideboard",bseq);
		} catch (SQLException e) {
			count=0;
			System.out.println("hideBoard : "+e);
		}
		return count;
	}

	@Override
	public ArrayList<BoardDto> noHideBoards(String email) {
		ArrayList<BoardDto> list= new ArrayList<BoardDto>();
		try {
			list = (ArrayList<BoardDto>)this.getSqlMapClient().queryForList("noHideBoards", email);
		} catch (SQLException e) {
			System.out.println("getUserBoards : "+e);
		}
		return list;
	}

	@Override
	public ArrayList<BoardDto> noHideOurBoards(String email,int page) {
		ArrayList<BoardDto> list = null;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("email", email);
		map.put("page", page);
		
		try {
			list = (ArrayList<BoardDto>)getSqlMapClient().queryForList("noHideOurboard",map);
		} catch (SQLException e) {
			System.out.println("noHideOurboard : "+e);
		}

		return list;
	}

	@Override
	public int sendPhoto(PhotosDto dto) {
		int count=1;
		try {
			this.getSqlMapClient().insert("sendPhoto",dto);
		} catch (SQLException e) {
            count=0;
            System.out.println("sendPhoto : "+e);
		}

		return count;
	}

	@Override
	public PhotosDto photoDetail(int imgseq) {
		PhotosDto pdto = new PhotosDto(); 
		try {
			pdto = (PhotosDto)this.getSqlMapClient().queryForObject("photoDetail",imgseq);
		} catch (SQLException e) {
			System.out.println("photoDetail : "+e);
		}
		return pdto;
	}

	@Override
	public int photoDel(int imgseq) {
		int count=1;
		try {
			this.getSqlMapClient().delete("photoDel",imgseq);
		} catch (SQLException e) {
			count=0;
			System.out.println("photoDel : "+e);
		}
		return count;
	}

	@Override
	public int imgWriteBoard(PhotosDto dto) {
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("membername", dto.getMembername());
		maps.put("imgcontent", dto.getImgcontent());
		maps.put("email", dto.getEmail());
		maps.put("image", dto.getImage());
		int count=1;
		try {
			this.getSqlMapClient().insert("imgWriteBoard",maps);
		} catch (SQLException e) {
			count=0;
			System.out.println("imgWriteBoard : "+e);
		}
		return count;
	}

	@Override
	public ArrayList<PhotosDto> getUserImgBoards(int parent) {
		ArrayList<PhotosDto> plist= new ArrayList<PhotosDto>();
		try {
			plist = (ArrayList<PhotosDto>)this.getSqlMapClient().queryForList("getUserImgBoards", parent);
		} catch (SQLException e) {
			System.out.println("getUserImgBoards : "+e);
		}
		return plist;
	}

	@Override
	public PhotosDto getImgBoard(int imgseq) {
		PhotosDto dto=null;
		try {
			dto=(PhotosDto)this.getSqlMapClient().queryForObject("getImgBoard",imgseq);
		} catch (SQLException e) {
			System.out.println("getImgBoard : "+e);
		}
		return dto;
	}

	@Override
	public int imgreply(PhotosDto dto) {
		int count=1;
		try {
			this.getSqlMapClient().insert("imgReply",dto);
		} catch (SQLException e) {
			count=0;
			System.out.println("imgreply : "+e);
		}
		return count;
	}

	@Override
	public int imgReplyDel(int imgseq) {
		int count=1;
		try {
			this.getSqlMapClient().delete("imgReplyDel",imgseq);
		} catch (SQLException e) {
			count=0;
			System.out.println("imgReplyDel : "+e);
		}
		return count;
	}


	@Override
	public int message(MessageDto dto) {
		int result=1;
		try {
			this.getSqlMapClient().insert("sendMessage", dto);
		} catch (SQLException e) {
			result=0;
			System.out.println("sendMessage : "+e);
		}
		return result;
	}


	@Override
	public ArrayList<MessageDto> sendMessagebox(MessageDto dto) {
		ArrayList<MessageDto> count = null;
		try {
			count=(ArrayList<MessageDto>)this.getSqlMapClient().queryForList("sendMessagebox", dto);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	return count;
	}


	@Override
	public ArrayList<MessageDto> receiveMessagebox(MessageDto dto) {
		ArrayList<MessageDto> count1 = null;
		try {
			count1=(ArrayList<MessageDto>)this.getSqlMapClient().queryForList("receiveMessagebox", dto);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	return count1;
	}


	@Override
	public int deleteMessage(MessageDto dto) {
		int count=1;
		try {
			this.getSqlMapClient().update("deleteMessage",dto);
		} catch (SQLException e) {
            count=0;
            System.out.println("deleteMessage : "+e);
		}
		return count;
	}


	@Override
	public int deleteMessage2(MessageDto dto) {
		int count=1;
		try {
			this.getSqlMapClient().update("deleteMessage2",dto);
		} catch (SQLException e) {
            count=0;
            System.out.println("deleteMessage2 : "+e);
		}
		return count;
	}


	@Override
	public int senddel(int mseq) {
		int count=1;
		try {
			this.getSqlMapClient().update("senddel",mseq);
		} catch (SQLException e) {
            count=0;
            System.out.println("senddel : "+e);
		}
		return count;
	}

}
